package com.xhm.bbs.service;

import com.xhm.bbs.dao.BbsDAO;
import com.xhm.bbs.dao.UserDAO;
import com.xhm.bbs.pojo.dto.BbsAddDTO;
import com.xhm.bbs.pojo.dto.BbsUpdateDTO;
import com.xhm.bbs.pojo.mapper.BbsMapper;
import com.xhm.bbs.pojo.po.BbsPO;
import com.xhm.bbs.pojo.qo.BbsQO;
import com.xhm.bbs.pojo.vo.BbsListVO;
import com.xhm.bbs.pojo.vo.BbsShowVO;
import com.xhm.common.constant.ErrorCode;
import com.xhm.common.exception.BusinessException;
import com.xhm.common.pojo.qo.OptionQO;
import com.xhm.common.pojo.vo.OptionVO;
import com.xhm.common.pojo.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 【帖子】增删改查服务
 *
 * @author mrj
 * @date 2020/01/01
 */
@Service
public class BbsService {

    @Autowired
    private BbsDAO bbsDAO;
    @Autowired
    private UserDAO userDAO;


    /**
     * 新增【帖子】
     *
     * @param bbsDTO
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public BbsPO save(BbsAddDTO bbsDTO) {
        BbsPO bbs = BbsMapper.INSTANCE.fromAddDTO(bbsDTO);
        if(bbs.getUserId() != null){
            Assert.isTrue(userDAO.exist(bbs.getUserId()),"用户ID有误");
        }
        bbsDAO.save(bbs);
        return bbs;
    }

    /**
     * 修改【帖子】
     *
     * @param bbsUpdateDTO
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public BbsPO update(BbsUpdateDTO bbsUpdateDTO) {
        Long id = bbsUpdateDTO.getId();
        BbsPO bbs = this.getBbs(id, true);
        BbsMapper.INSTANCE.setUpdateDTO(bbs, bbsUpdateDTO);
        if(bbs.getUserId() != null){
            Assert.isTrue(userDAO.exist(bbs.getUserId()),"用户ID有误");
        }
        bbsDAO.update(bbs);
        return bbs;
    }

    /**
     * 查询分页列表
     *
     * @param bbsQO
     * @return
     */
    public PageVO<BbsListVO> list(BbsQO bbsQO) {
        PageVO<BbsListVO> page = bbsDAO.findByPage(bbsQO);
        return page;
    }

    /**
     * 查询【帖子】选项列表
     *
     * @return
     */
    public List<OptionVO<Long, String>> findOptions(OptionQO<Long, String> qo) {
        List<OptionVO<Long, String>> options = bbsDAO.findOptions(qo);
        return options;
    }

    /**
     * 根据主键获取【帖子】
     *
     * @param id 主键
     * @param force 是否强制获取
     * @return
     */
    public BbsPO getBbs(Long id, boolean force){
        BbsPO bbs = bbsDAO.findById(id);
        if (force && bbs == null) {
            throw new BusinessException(ErrorCode.RECORD_NOT_FIND);
        }
        return bbs;
    }


    /**
     * 查询【帖子】详情
     *
     * @param id
     * @return
     */
    public BbsShowVO show(Long id) {
        BbsPO bbs = this.getBbs(id, true);
        BbsShowVO showVO = BbsMapper.INSTANCE.toShowVO(bbs);
        return showVO;
    }

    /**
     * 删除【帖子】
     *
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public int delete(Long... ids) {
        int count = 0;
        for (Long id : ids) {
            count += bbsDAO.delete(id);
        }
        return count;
    }


}


